package com.huawei.appgallery.forum.cards.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.DynamicLayout;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.TextUtils.TruncateAt;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import androidx.appcompat.widget.ActivityChooserView.ActivityChooserViewAdapter;
import com.huawei.appgallery.forum.base.card.bean.Reply;
import com.huawei.appgallery.forum.base.card.bean.User;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.reflect.Field;
import java.security.AccessController;
import java.security.PrivilegedAction;
import o.bqx;
import o.bqy;
import o.bsy.e;
import o.bsy.j;
import o.buc;
import o.bug;
import o.fo;

public class ReplyTextView extends TextView {
    private int ˊ = 0;
    private boolean ˋ = false;
    private boolean ˎ = false;
    private int ˏ;
    private int ॱ = 0;

    public class a extends ClickableSpan {
        private b ˊ;
        private boolean ˋ = false;
        private OnClickListener ˎ;
        final /* synthetic */ ReplyTextView ˏ;
        private boolean ॱ;
        private boolean ॱॱ = false;

        public void ॱ(boolean z) {
            this.ˋ = z;
        }

        public void ˊ(boolean z) {
            this.ॱ = z;
            if (this.ˊ != null) {
                this.ˊ.ˊ(this.ॱ);
            }
        }

        public void ˋ(b bVar) {
            this.ˊ = bVar;
        }

        public a(ReplyTextView replyTextView, OnClickListener onClickListener) {
            this.ˏ = replyTextView;
            this.ˎ = onClickListener;
        }

        public void updateDrawState(TextPaint textPaint) {
            textPaint.setUnderlineText(false);
            if (this.ˋ) {
                textPaint.setColor(fo.ˋ(this.ˏ.getContext(), o.bsy.d.ʻ));
            }
            textPaint.bgColor = this.ˏ.getContext().getResources().getColor(ˏ());
        }

        private int ˏ() {
            if (!this.ॱ || !this.ˋ) {
                return o.bsy.d.ˏॱ;
            }
            if (this.ॱॱ) {
                return o.bsy.d.ʼ;
            }
            return o.bsy.d.ʽ;
        }

        public void onClick(View view) {
            if (this.ˎ != null) {
                this.ˎ.onClick(view);
            }
        }

        public void ˎ(boolean z) {
            this.ॱॱ = z;
        }
    }

    public interface b {
        void ˊ(boolean z);
    }

    public static class d extends LinkMovementMethod {
        private boolean ˎ;
        private a ˏ;

        d(boolean z) {
            this.ˎ = z;
        }

        public boolean onTouchEvent(TextView textView, Spannable spannable, MotionEvent motionEvent) {
            if (motionEvent.getAction() == 0) {
                this.ˏ = ॱ(textView, spannable, motionEvent);
                if (this.ˏ != null) {
                    this.ˏ.ˊ(true);
                    this.ˏ.ˎ(this.ˎ);
                    Selection.setSelection(spannable, spannable.getSpanStart(this.ˏ), spannable.getSpanEnd(this.ˏ));
                }
            } else if (2 == motionEvent.getAction()) {
                a ॱ = ॱ(textView, spannable, motionEvent);
                if (!(this.ˏ == null || ॱ == this.ˏ)) {
                    this.ˏ.ˊ(false);
                    this.ˏ.ˎ(this.ˎ);
                    this.ˏ = null;
                    Selection.removeSelection(spannable);
                }
            } else {
                Selection.removeSelection(spannable);
                if (this.ˏ == null) {
                    return true;
                }
                this.ˏ.ˊ(false);
                this.ˏ.ˎ(this.ˎ);
                this.ˏ = null;
            }
            return super.onTouchEvent(textView, spannable, motionEvent);
        }

        private a ॱ(TextView textView, Spannable spannable, MotionEvent motionEvent) {
            int x = (((int) motionEvent.getX()) - textView.getTotalPaddingLeft()) + textView.getScrollX();
            int y = (((int) motionEvent.getY()) - textView.getTotalPaddingTop()) + textView.getScrollY();
            Layout layout = textView.getLayout();
            x = layout.getOffsetForHorizontal(layout.getLineForVertical(y), (float) x);
            a[] aVarArr = (a[]) spannable.getSpans(x, x, a.class);
            if (aVarArr.length > 0) {
                return aVarArr[0];
            }
            return null;
        }
    }

    public ReplyTextView(Context context) {
        super(context);
    }

    public ReplyTextView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    protected void onMeasure(int i, int i2) {
        if (ˋ()) {
            Field field = null;
            try {
                Field declaredField = DynamicLayout.class.getDeclaredField("sStaticLayout");
                ˋ(declaredField);
                StaticLayout staticLayout = (StaticLayout) declaredField.get(DynamicLayout.class);
                if (staticLayout != null) {
                    field = StaticLayout.class.getDeclaredField("mMaximumVisibleLineCount");
                    ˋ(field);
                    field.setInt(staticLayout, getMaxLines());
                }
                super.onMeasure(i, i2);
                if (staticLayout != null && field != null) {
                    field.setInt(staticLayout, ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED);
                    return;
                }
                return;
            } catch (Throwable e) {
                bqx.ˊ("ReplyTextView", "onMeasure Exception", e);
                return;
            }
        }
        super.onMeasure(i, i2);
    }

    public void setData(Reply reply, OnClickListener onClickListener, OnClickListener onClickListener2, OnClickListener onClickListener3, b bVar) {
        String replace;
        SpannableString spannableString = null;
        User ˎ = reply.ˎ();
        User ॱ = reply.ॱ();
        if (ˋ()) {
            replace = reply.ˋ().replace("[br]", HwAccountConstants.BLANK);
        } else {
            replace = reply.ˋ().replace("[br]", "\n");
        }
        int ॱ2;
        if (ˎ != null && !TextUtils.isEmpty(ˎ.ˋ()) && ॱ != null && !TextUtils.isEmpty(ॱ.ˋ())) {
            spannableString = new SpannableString(getContext().getString(j.ॱ, new Object[]{ॱ.ˋ() + ˎ(ॱ) + ˏ(ॱ), ˎ.ˋ() + ˎ(ˎ) + ˏ(ˎ), replace}));
            ॱ2 = ॱ(ॱ, spannableString, 0, onClickListener);
            int ॱ3 = ॱ(ॱ, spannableString, ॱ2);
            ॱ2 = ॱ(ॱ, ॱ3, ˋ(ॱ, spannableString, ॱ2 + ॱ3));
            int ॱ4 = ॱ(ˎ, spannableString, ॱ2, onClickListener2);
            ॱ3 = ॱ(ˎ, spannableString, ॱ2 + ॱ4);
            ˎ(spannableString, ((ॱ2 + ॱ4) + ॱ3) + ˋ(ˎ, spannableString, (ॱ2 + ॱ4) + ॱ3), onClickListener3, bVar);
            setText(spannableString);
        } else if (ॱ == null || TextUtils.isEmpty(ॱ.ˋ())) {
            setVisibility(8);
        } else {
            spannableString = new SpannableString(getContext().getString(j.ˊ, new Object[]{ॱ.ˋ() + ˎ(ॱ) + ˏ(ॱ), replace}));
            ॱ2 = ॱ(ॱ, spannableString, 0, onClickListener);
            int ॱ5 = ॱ(ॱ, spannableString, ॱ2);
            ˎ(spannableString, (ॱ2 + ॱ5) + ˋ(ॱ, spannableString, ॱ2 + ॱ5), onClickListener3, bVar);
            setText(spannableString);
        }
        setMovementMethod(new d(ˏ()));
        setHighlightColor(getContext().getResources().getColor(o.bsy.d.ˏॱ));
        ˎ(spannableString);
    }

    private void ˎ(SpannableString spannableString) {
        if (spannableString != null && ˋ()) {
            Paint paint = new Paint();
            paint.setTextSize(getTextSize());
            setText(TextUtils.ellipsize(spannableString, getPaint(), (float) ((int) (((float) (this.ˏ * getMaxLines())) - paint.measureText("..."))), TruncateAt.END));
        }
    }

    public void setMaxWidth(int i) {
        this.ˏ = i;
    }

    private String ˎ(User user) {
        if (ॱ(user)) {
            return "EMPTYFLAGIMGFLAGEMPTYFLAG";
        }
        return "";
    }

    private boolean ॱ(User user) {
        return user.ʻ() || user.ᐝ();
    }

    private boolean ˋ() {
        int maxLines = getMaxLines();
        return maxLines > 0 && maxLines < ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED && this.ˏ > 0;
    }

    private void ˋ(final Field field) {
        AccessController.doPrivileged(new PrivilegedAction(this) {
            final /* synthetic */ ReplyTextView ॱ;

            public Object run() {
                field.setAccessible(true);
                return null;
            }
        });
    }

    public void setShowModeratorStamp(boolean z) {
        this.ˎ = z;
    }

    public void setShowHostStamp(boolean z) {
        this.ˋ = z;
    }

    public void setHostPriority(int i) {
        this.ॱ = i;
    }

    public int ˊ() {
        return this.ˊ;
    }

    public void setModeratorStampPriority(int i) {
        this.ˊ = i;
    }

    private String ˏ(User user) {
        if (!ˋ(user)) {
            return "";
        }
        if (ॱ(user)) {
            return "STAMPFLAGEMPTYFLAG";
        }
        return "EMPTYFLAGSTAMPFLAGEMPTYFLAG";
    }

    private boolean ˋ(User user) {
        return user != null && ((this.ˋ && user.ʽ()) || (this.ˎ && user.ॱॱ()));
    }

    private int ॱ(User user, SpannableString spannableString, int i, OnClickListener onClickListener) {
        a aVar = new a(this, onClickListener);
        aVar.ॱ(true);
        int length = user.ˋ().length();
        spannableString.setSpan(aVar, i, i + length, 17);
        return length;
    }

    private void ˎ(SpannableString spannableString, int i, OnClickListener onClickListener, b bVar) {
        a aVar = new a(this, onClickListener);
        aVar.ॱ(false);
        aVar.ˋ(bVar);
        spannableString.setSpan(aVar, i, spannableString.length(), 17);
    }

    private int ॱ(User user, SpannableString spannableString, int i) {
        Object obj = null;
        if (user.ʻ()) {
            Drawable drawable = getContext().getResources().getDrawable(o.bsy.a.ʻ);
            drawable.setBounds(0, 0, (int) getContext().getResources().getDimension(e.ͺ), (int) getContext().getResources().getDimension(e.ͺ));
            obj = new buc(drawable);
        }
        if (user.ᐝ()) {
            drawable = getContext().getResources().getDrawable(o.bsy.a.ॱॱ);
            drawable.setBounds(0, 0, (int) getContext().getResources().getDimension(e.ͺ), (int) getContext().getResources().getDimension(e.ͺ));
            obj = new buc(drawable);
        }
        if (obj == null) {
            return 0;
        }
        int ॱ = ॱ(spannableString, i);
        int i2 = i + ॱ;
        int length = "IMGFLAG".length();
        spannableString.setSpan(obj, i2, i2 + length, 17);
        return ॱ(spannableString, i2 + length) + (ॱ + length);
    }

    private int ˋ(User user, SpannableString spannableString, int i) {
        int i2 = 1;
        if (!ˋ(user)) {
            return 0;
        }
        int i3;
        int i4;
        if (ॱ(user)) {
            i3 = 0;
        } else {
            i3 = ॱ(spannableString, i);
        }
        int i5 = (this.ˋ && user.ʽ()) ? 1 : 0;
        if (this.ˎ && user.ॱॱ()) {
            i4 = 1;
        } else {
            i4 = 0;
        }
        if (i5 == 0 || i4 == 0) {
            i2 = i4;
            i4 = i5;
        } else {
            if (this.ॱ > this.ˊ) {
                i5 = 1;
            } else {
                i5 = 0;
            }
            if (i5 != 0) {
                i2 = 0;
            }
            i4 = i5;
        }
        i5 = -1;
        if (i4 != 0) {
            i5 = 11;
        } else if (i2 != 0) {
            i5 = 10;
        }
        Drawable o_bug = new bug(bqy.ˋ().ˏ());
        o_bug.ॱ(i5);
        o_bug.draw(new Canvas());
        o_bug.setBounds(0, 0, (int) ((float) o_bug.getIntrinsicWidth()), (int) getResources().getDimension(e.ˈ));
        buc o_buc = new buc(o_bug);
        i5 = "STAMPFLAG".length();
        spannableString.setSpan(o_buc, i + i3, (i + i3) + i5, 17);
        return ॱ(spannableString, (i + i3) + i5) + (i3 + i5);
    }

    private int ॱ(User user, int i, int i2) {
        return (getContext().getString(j.ॱ, new Object[]{user.ˋ(), user.ˋ(), ""}).lastIndexOf(user.ˋ()) + i) + i2;
    }

    private int ॱ(SpannableString spannableString, int i) {
        Drawable drawable = getResources().getDrawable(o.bsy.a.ͺ);
        drawable.setBounds(0, 0, (int) getResources().getDimension(e.ʼॱ), (int) getResources().getDimension(e.ʾ));
        spannableString.setSpan(new buc(drawable), i, "EMPTYFLAG".length() + i, 17);
        return "EMPTYFLAG".length();
    }

    boolean ˏ() {
        return false;
    }
}
